/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现Xml的序列化和反序列化
* CreateTime：2020-04-05 21:53:37
* Version：1.0
* Modify Recorder：
*************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TinyToolKit
{
    public static partial class TinyUtility
    {
        public static partial class XML
        {
            private static IXmlTemplate _template;

            private static IXmlTemplate _xmlTemplate
            {
                get => _template ?? (_template = new DefaultXmlTemplate());
                set => _template = value;
            }

            public static void SetXmlTemplate(IXmlTemplate xmlTemplate)
            {
                _xmlTemplate = xmlTemplate;
            }

            /// <summary>
            /// 将对象序列化为XML，并保存在目标路径
            /// </summary>
            /// <param name="savePath">序列化后XMl保存的路径</param>
            /// <param name="obj">需要序列化的对象</param>
            public static void XmlSerialize(string savePath, object obj)
            {
                if (string.IsNullOrEmpty(savePath) || obj == null) return;
                if (_xmlTemplate == null)
                    TLogTool.Error("Xml的默认函数集模板不能为Null");

                _xmlTemplate?.XmlSerialize(savePath, obj);
            }

            /// <summary>
            /// 将目标路径的XML文件反序列化为对象
            /// </summary>
            /// <typeparam name="T">需要反序列化的对象类型</typeparam>
            /// <param name="targetPath">XML文件存储的目标路径</param>
            /// <returns>反序列化后的类</returns>
            /// <returns>反序列化后的类</returns>
            /// <returns>反序列化后的类</returns>
            /// <returns>反序列化后的类</returns>
            public static T XmlDeSerialize<T>(string targetPath) where T : class
            {
                if (string.IsNullOrEmpty(targetPath)) return default(T);
                if (_xmlTemplate == null)
                    TLogTool.Error("Xml的默认函数集模板不能为Null");

                return _xmlTemplate?.XmlDeSerialize<T>(targetPath);
            }

            /// <summary>
            /// 将目标路径的XML文件反序列化为对象
            /// </summary>
            /// <param name="objectType">需要反序列化的对象类型</param>
            /// <param name="targetPath">XML文件存储的目标路径</param>
            /// <returns>反序列化后的类</returns>
            public static object XmlDeSerialize(Type objectType, string targetPath)
            {
                if (string.IsNullOrEmpty(targetPath) || objectType == null) return null;
                if (_xmlTemplate == null)
                    TLogTool.Error("Xml的默认函数集模板不能为Null");

                return _xmlTemplate?.XmlDeSerialize(objectType,targetPath);
            }
        }
    }
}
